// 中介者模式（Mediator Pattern）
// 中介者模式用一个中介对象来封装一系列对象的交互，使对象彼此之间松散耦合，而且可以改变它们之间的交互。
// 同事类  
class Colleague {  
  constructor(mediator, name) {  
    this.mediator = mediator;  
    this.name = name;  
  }  
  
  send(message) {  
    this.mediator.notify(this, message);  
  }  
  
  receive(message) {  
    console.log(`${this.name} received message: ${message}`);  
  }  
}  
  
// 中介者  
class Mediator {  
  constructor() {  
    this.colleagues = {};  
  }  
  
  register(colleague, name) {  
    this.colleagues[name] = colleague;  
  }  
  
  notify(colleague, message) {  
    for (const name in this.colleagues) {  
      if (this.colleagues[name] !== colleague) {  
        this.colleagues[name].receive(message);  
      }  
    }  
  }  
}  
  
// 使用中介者模式  
const mediator = new Mediator();  
  
const colleagueA = new Colleague(mediator, 'Colleague A');  
const colleagueB = new Colleague(mediator, 'Colleague B');  
const colleagueC = new Colleague(mediator, 'Colleague C');  
  
mediator.register(colleagueA, 'A');  
mediator.register(colleagueB, 'B');  
mediator.register(colleagueC, 'C');  
  
colleagueA.send('Hello everyone!'); // B 和 C 会收到消息